package com.bw.ae.nexus.service;

import java.io.IOException;

import com.bw.ae.nexus.Exceptions.*;
import com.bw.ae.nexus.INexusOperator;
import com.bw.ae.nexus.domain.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by Xiaosiyuan on 2016/8/24.
 */
@Service
public class NexusOperatorService implements INexusOperator {

    @Autowired
    private NexusFunction nexusFunction;

    @Override
    public boolean createRepository(String projectLabel) {
        try {
            nexusFunction.createRepo(projectLabel);
            nexusFunction.addRoles(projectLabel);
            nexusFunction.updateRepo(projectLabel);
            return true;
        } catch (RepositoryCreateException e) {
            return false;
        } catch (RepositoryUpdateException e) {
            nexusFunction.deleteRepository(projectLabel);
            nexusFunction.deleteRole(projectLabel);
            return false;
        } catch (RoleAddException e) {
            nexusFunction.deleteRepository(projectLabel);
            return false;
        }
    }

    @Override
    public boolean addUser(User user) {
        try
        {
            nexusFunction.addUser(user);
            return true;
        }catch(UserAddException e)
        {
            return false;
        }

    }

    @Override
    public boolean grantUser(String userId, String projectLabel) {

        try {
            User user=new User();
            user.setUserId(userId);
            nexusFunction.updateUser(user,projectLabel);
            return true;
        }catch (UserGrantException e)
        {
            return false;
        }

    }

    @Override
    public boolean changePassword(String userId, String oldPassword, String newPassword) {
        try {
            nexusFunction.changePassword(userId,oldPassword,newPassword);
            return true;
        }catch (PasswordChangeException e)
        {
            return false;
        }
    }

    @Override
    public boolean changePassword(String userId, String password) {
        try {
            nexusFunction.changePassword(userId,password);
            return true;
        } catch (PasswordChangeException e) {
            return false;
        }

    }

    @Override
    public boolean deleteUser(String userId) {
        try {
            nexusFunction.deleteUser(userId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean ungrantUser(String userId, String projectLabel) {
        try {
            nexusFunction.ungrantUser(userId,projectLabel);
            return true;
        }catch (UserUnGrantException e)
        {
            return false;
        }

    }

	@Override
	public boolean disableUser(String userId) {
		// TODO Auto-generated method stub
		
		try {
			nexusFunction.disableUser(userId);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return false;
		}
		
	}

	@Override
	public boolean activeUser(String userId) {
		// TODO Auto-generated method stub
		try {
			nexusFunction.activeUser(userId);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return false;
		}
		
	}
}
